//
//  CustomViewControllerTests.m
//  AmountBodyTests
//
//  Created by Qiu Jishuai on 2018/1/19.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "StubCustomViewController.h"

#define kViewH ([UIScreen mainScreen].bounds.size.height - 49.0f - 64.0f)

@interface CustomViewControllerTests : XCTestCase
@property (nonatomic, strong) StubCustomViewController *vc;
@property (nonatomic, strong) UINavigationController *nav;
@end

@implementation CustomViewControllerTests

- (void)setUp {
    [super setUp];
    self.vc = [[StubCustomViewController alloc] init];
    self.nav = [[UINavigationController alloc] initWithRootViewController:self.vc];
    self.nav.view.frame = [UIScreen mainScreen].bounds;
    _vc.view.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 64 - 49);
    [_vc viewDidLoad];
    [_vc viewDidLayoutSubviews];
}

- (void)tearDown {
    self.nav = nil;
    self.vc = nil;
    [super tearDown];
}

//上面的  将对应的vc移动到第一个位置 原有的第一成为二, 第二成为3 修改offset到1的位置
- (void)testSwitchToSmall {
//     for (NSInteger i = 3; i >= 0; i --) {
    //4 - 3

        self.vc.switchView.selectedIndex = 0;
        [self.vc.scrollView setContentOffset:CGPointMake(0, kViewH) animated:YES];
        [self.vc.scrollView setContentOffset:CGPointMake(0, kViewH) animated:YES];
        [self.vc.scrollView setContentOffset:CGPointMake(0, kViewH) animated:YES];
        [self.vc.scrollView setContentOffset:CGPointMake(0, kViewH) animated:YES];

         NSInteger i = 3;
         NSInteger currentType = i + 1;
         self.vc.isScrollToPrevious = NO;
         self.vc.isScrollBySwitch = NO;
         self.vc.switchView.selectedIndex = currentType;
         self.vc.currentType = currentType;
         [self.vc handleSwitchIndex:currentType index:i];

         XCTAssertFalse(self.vc.isScrollToPrevious);
         XCTAssertTrue(CGPointEqualToPoint(self.vc.scrollView.contentOffset, CGPointZero));
         XCTAssertTrue(self.vc.isScrollBySwitch);
         XCTAssertEqual(self.vc.scrollView.subviews.count, 3);
         XCTAssertEqual(self.vc.switchView.selectedIndex, i);
         XCTAssertEqual(self.vc.currentType, i);
//     }
}


- (void)testSwitchToLarge {
    for (NSInteger i = 1; i < 5; i ++) {
        self.vc.switchToLargeIndex = NO;
        self.vc.isScrollBySwitch = NO;
        self.vc.switchView.selectedIndex = i;
        NSInteger currentType = 0;
        [self.vc handleSwitchIndex:currentType index:i];

        XCTAssertFalse(self.vc.switchToLargeIndex);
        XCTAssertTrue(CGPointEqualToPoint(self.vc.scrollView.contentOffset, CGPointZero));
        XCTAssertTrue(self.vc.isScrollBySwitch);

        if (i == CustomSubviewControllerTypeOrderInfo) {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 2);
        } else {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 3);
        }

        UIViewController *currentVc = [self.vc vcWithType:i]; XCTAssertTrue(CGRectEqualToRect(currentVc.view.frame,self.vc.scrollView.frame));

        XCTAssertEqual(self.vc.switchView.selectedIndex, i);
        XCTAssertEqual(self.vc.currentType, i);

        UIViewController *nextVc = [self.vc vcWithType:i + 1];
//        if (nextVc) {
//            XCTAssertTrue(CGRectEqualToRect(nextVc.view.frame, CGRectMake(0, kViewH, self.vc.scrollView.width, self.vc.scrollView.height)));
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, kViewH * 2)));
//        } else {
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, kViewH)));
//        }
    }
}

- (void)testSwithToNext {
    for (NSInteger i = 1; i < 5; i ++) {
        self.vc.isScrollToNext = NO;
        self.vc.isScrollBySwitch = NO;
        self.vc.switchView.selectedIndex = i;
        NSInteger currentType = i - 1;
        [self.vc handleSwitchIndex:currentType index:i];

        XCTAssertFalse(self.vc.isScrollToNext);
        XCTAssertTrue(CGPointEqualToPoint(self.vc.scrollView.contentOffset, CGPointZero));
        XCTAssertTrue(self.vc.isScrollBySwitch);

        if (i == CustomSubviewControllerTypeOrderInfo) {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 2);
        } else {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 3);
        }

        UIViewController *currentVc = [self.vc vcWithType:i]; XCTAssertTrue(CGRectEqualToRect(currentVc.view.frame,self.vc.scrollView.frame));

        XCTAssertEqual(self.vc.switchView.selectedIndex, i);
        XCTAssertEqual(self.vc.currentType, i);

        UIViewController *nextVc = [self.vc vcWithType:i + 1];
//        if (nextVc) {
//            XCTAssertTrue(CGRectEqualToRect(nextVc.view.frame, CGRectMake(0, kViewH, self.vc.scrollView.width, self.vc.scrollView.height)));
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, kViewH * 2)));
//        } else {
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, kViewH)));
//        }
    }
}

- (void)testSwitchToNextIsCalled {
    self.vc.switchView.selectedIndex = 1;
    NSInteger currentType = CustomSubviewControllerTypeStyle;
    [self.vc handleSwitchIndex:currentType index:1];
    XCTAssertTrue(self.vc.isSwitchToNext);
}

- (void)testSwitchToLargeIsCalled {
    self.vc.switchView.selectedIndex = 4;
    NSInteger currentType = CustomSubviewControllerTypeStyle;
    [self.vc handleSwitchIndex:currentType index:4];
    XCTAssertTrue(self.vc.switchToLargeIndex);
}

- (void)testSwitchToSmallIsCalled {
    self.vc.switchView.selectedIndex = 4;
    NSInteger currentType = CustomSubviewControllerTypeOrderInfo;
    [self.vc handleSwitchIndex:currentType index:0];
    XCTAssertTrue(self.vc.switchToSmallIndex);
}

//滑动到下一个结束 1.第一个移除  第二个移到第一个位置 修改offset为0
//2.如果有下一个  且 下一个可用   加载第二个
- (void)testScrollViewDragToNext {
    CGFloat viewH = [UIScreen mainScreen].bounds.size.height - 49 -64;
    CGPoint offset = CGPointMake(0, viewH);

    for (NSInteger i = 1; i <= CustomSubviewControllerTypeOrderInfo; i ++) {
        self.vc.scrollView.contentOffset = offset;
        [self.vc scrollViewDidEndDecelerating:self.vc.scrollView];

        UIViewController *lastVc = [self.vc vcWithType:i - 1];
        XCTAssertNil(lastVc.view.superview);

        UIViewController *currentVc = [self.vc vcWithType:i];

        UIViewController *nextVc = [self.vc vcWithType:i + 1];
//        if (nextVc) {
//              XCTAssertTrue(CGRectEqualToRect(nextVc.view.frame, CGRectMake(0, viewH, self.vc.scrollView.width, self.vc.scrollView.height)));
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, viewH * 2)));
//        } else {
//            XCTAssertTrue(CGSizeEqualToSize(self.vc.scrollView.contentSize, CGSizeMake(self.vc.view.width, viewH)));
//        }
        XCTAssertTrue(CGRectEqualToRect(currentVc.view.frame,self.vc.scrollView.frame));

        XCTAssertEqual(self.vc.switchView.selectedIndex, i);
        XCTAssertEqual(self.vc.currentType, i);
        if (i == CustomSubviewControllerTypeOrderInfo) {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 2);
        } else {
            XCTAssertEqual(self.vc.scrollView.subviews.count, 3);
        }//2 + 2
    }
}


- (void)testViewDidLoadedVc {
    XCTAssertEqual(self.vc.styleVC, self.vc.childViewControllers.firstObject);
    XCTAssertEqual(self.vc.designVC, self.vc.childViewControllers.lastObject);
    XCTAssertEqual(self.vc.childViewControllers.count, 2);
    XCTAssertEqual(self.vc.switchView.selectedIndex, 0);
    XCTAssertEqual(self.vc.currentType, CustomSubviewControllerTypeStyle);
}

- (void)testAddVc {
    CustomBaseSubViewController *vc = [CustomBaseSubViewController new];
    [self.vc addVc: vc];
    XCTAssertTrue(CGRectEqualToRect(self.vc.scrollView.frame, vc.view.frame));
    XCTAssertNotNil(vc.view.superview);
}


- (void)testSubVcTypesEquals {
    XCTAssertEqual(self.vc.styleVC.type, CustomSubviewControllerTypeStyle);
    XCTAssertEqual(self.vc.designVC.type, CustomSubviewControllerTypeDesign);
    XCTAssertEqual(self.vc.depathDesignVC.type, CustomSubviewControllerTypeDepthDesign);
    XCTAssertEqual(self.vc.measureVC.type, CustomSubviewControllerTypeMeasure);
    XCTAssertEqual(self.vc.infoVC.type, CustomSubviewControllerTypeOrderInfo);
}

- (void)testSubVcTargesEquals {
    XCTAssertEqual(self.vc.styleVC.target, CustomSubviewControllerTypeDesign);
    XCTAssertEqual(self.vc.designVC.target, CustomSubviewControllerTypeDepthDesign);
    XCTAssertEqual(self.vc.depathDesignVC.target, CustomSubviewControllerTypeMeasure);
    XCTAssertEqual(self.vc.measureVC.target, CustomSubviewControllerTypeOrderInfo);
}



@end
